﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.IO;
using System.Windows.Threading;
using System.ComponentModel;
using System.Reflection;

namespace AzureDriverExplorer_Client
{
    /// <summary>
    /// Interaction logic for Reload.xaml
    /// </summary>
    public partial class Reload : Window
    {
       System.Threading.Thread thread;
        const int DEFAULT_TIME = 4000;
        private bool Cancel = false;
        //Azure
        private static GetListVHD.AzureDriveExplorerServiceSoapClient list = new GetListVHD.AzureDriveExplorerServiceSoapClient();
        public GetListVHD.ArrayOfString ListVHD = null;

        Thread t;
        public Reload()
        {
            InitializeComponent();
           
        }


        private void closeButton_Click(object sender, RoutedEventArgs e)
        {
            Cancel = true;
            // Wait for 3sec
            Thread.Sleep(3000);

            // Stop Thread
            thread.Abort();
            this.DialogResult = false;// System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        void fSplashScreen_Shown(object sender, EventArgs e)
        {
            this.pictureBoxLoading.Image = Properties.Resources.ajax_loader;

            initvalues();
            t = new System.Threading.Thread(initApplication);
            t.Start();
        }      


        private void initvalues()
        {

            this.label1.Content = String.Format("{0} {1}", Assembly.GetExecutingAssembly().GetName().Name.ToString(), Assembly.GetExecutingAssembly().GetName().Version.ToString());
            this.label2.Content = "Initializing...";
        }

        private void initApplication()
        {

            thread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate() { GetListAzure("Connecting to Azure..."); }));
            thread.Start();

        }

        private void setMessage(string msg)
        {
            label2.Content = msg;
        }

        private void GetListAzure(string msg)
        {
            //Azure
            GetListVHD.ArrayOfString vecFileAccount = new GetListVHD.ArrayOfString();

            label2.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(
                 delegate()
                 {
                     label2.Content = msg;
                 }
                 ));


            List<string> ListFileAccount = new List<string>();
            //read file account
            try
            {
                if (!File.Exists(Const.FileConfig))
                    File.Create(Const.FileConfig);
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader(Const.FileConfig))
                {
                    String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null)
                    {
                        vecFileAccount.Add(line);
                    }
                }

                //listDirectory = list.getDirectory();
                //listFiles = list.getFiles();
                ListVHD = list.getVHD(vecFileAccount, Const.key());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, string.Empty, MessageBoxButton.OK, MessageBoxImage.Error);
            }

            label1.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(
                delegate()
                {
                    label2.Content = "Loading...";
                }
                ));

            Thread.Sleep(DEFAULT_TIME);
            if (!Cancel)
                finishProcess();

        }

        private void finishProcess()
        {
            try
            {
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(
                     delegate()
                     {
                         try
                         {
                             this.DialogResult = true;
                         }
                         catch
                         { }
                     }
                     ));

                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(
                     delegate()
                     {
                         try
                         {
                             this.Close();
                         }
                         catch
                         { }
                     }
                     ));
            }
            catch
            { }
        }

    }
}
